package com.lfk.justweengine.utils.database;

import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.app.Context;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;

public class DataBaseOhosTest {

    @Test
    public void testInitAndOpen() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        DataBase dataBase = DataBase.initAndOpen("User", User.class);
        dataBase.open(context);
        dataBase.clear();
        Node node = new Node("Wang");
        dataBase.insert(node);
        int size = dataBase.size();
        dataBase.close();
        Assert.assertEquals(1, size);
    }

    @Test
    public void testUpdate() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        DataBase dataBase = DataBase.initAndOpen("User", User.class);
        dataBase.open(context);
        dataBase.clear();
        Node node = new Node("Zhao");
        dataBase.insert(node);
        node = new Node("Wang");
        dataBase.insert(node);
        node = new Node("Liu");
        node.key = 1;
        dataBase.update(node);
        List<Node> list = dataBase.query();
        dataBase.close();
       Assert.assertEquals(2,list.size());
    }

    @Test
    public void testDelete() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        DataBase dataBase = DataBase.initAndOpen("User", User.class);
        dataBase.open(context);
        dataBase.clear();
        Node node = new Node("Zhao");
        dataBase.insert(node);
        node = new Node("Wang");
        dataBase.insert(node);
        dataBase.delete(1);
        int size = dataBase.size();
        dataBase.close();
        Assert.assertEquals(1, size);
    }

    @Test
    public void testClear() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        DataBase dataBase = DataBase.initAndOpen("User", User.class);
        dataBase.open(context);
        dataBase.clear();
        Node node = new Node("Zhao");
        dataBase.insert(node);
        node = new Node("Wang");
        dataBase.insert(node);
        dataBase.clear();
        int size = dataBase.size();
        dataBase.close();
        Assert.assertEquals(0, size);
    }

    @Test
    public void testGet() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        DataBase dataBase = DataBase.initAndOpen("User", User.class);
        dataBase.open(context);
        dataBase.clear();
        Node node = new Node("Zhao");
        dataBase.insert(node);
        node = new Node("Wang");
        dataBase.insert(node);
        List<Node> list = dataBase.get(1);
        dataBase.close();
        Assert.assertEquals(1,list.size());
    }

    @Test
    public void testTestGet() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        DataBase dataBase = DataBase.initAndOpen("User", User.class);
        dataBase.open(context);
        dataBase.clear();
        Node node = new Node("Zhao");
        dataBase.insert(node);
        node = new Node("Wang");
        dataBase.insert(node);
        List<Node> list = dataBase.get(Long.valueOf(2));
        dataBase.close();
        Assert.assertEquals(1,list.size());
    }

    @Test
    public void testTestQuery1() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        DataBase dataBase = DataBase.initAndOpen("User", User.class);
        dataBase.open(context);
        dataBase.clear();
        Node node = new Node("Zhao");
        dataBase.insert(node);
        node = new Node("Wang");
        dataBase.insert(node);
        node = new Node("Liu");
        dataBase.insert(node);
        List<Node> list = dataBase.query(1,2);
        dataBase.close();
        Assert.assertEquals(2,list.size());

    }
}